import 'package:flutter/cupertino.dart';
import 'package:flutter_menu/app_config.dart';
import 'package:get/get.dart';

import 'login_controller.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:flutter_menu/gen/assets.gen.dart';
import 'dart:math' as math;

class LoginPage extends GetView<LoginController> {
  LoginPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LoginController>(
        init: controller, builder: (controller) => body(context).whitScaffold()).unPop();
  }

  Widget body(BuildContext context) {
    var country = AppConfig.instance.country;
    Widget content = country == null ? _choseCountry(context) : _loginWidget(context);
    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(image: MyAssets.images.bgLogin.provider(), fit: BoxFit.cover)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          MyAssets.images.icLoginLogo.image().size(width: 320.w).marginOnly(top: 104.h),
          Text(
            "login_title".tr,
            style: TextStyle(color: AppColors.white, fontWeight: FontWeight.bold, fontSize: 43.sp),
          ).marginOnly(top: 22.h),
          content,
          const Spacer(),
          Obx(() => Text(
                controller.version.value,
                style: TextStyle(fontSize: AppDimen.sp_22, color: Colors.white),
              ).marginOnly(bottom: 9.w)),
          Text(
            "${country?.url.tr} ${country?.phone.tr}",
            style: TextStyle(fontSize: AppDimen.sp_14, color: Colors.white),
          ).marginOnly(bottom: 52.w)
        ],
      ).size(width: double.infinity),
    );
  }

  Widget _choseCountry(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
       Builder(builder: (ctx){
         return  Stack(children: [
           Container(
             height: 61.w,
             width: double.infinity,
             decoration: BoxDecoration(
                 color: AppColors.white, borderRadius: BorderRadius.all(Radius.circular(10.w))),
             padding: EdgeInsets.symmetric(horizontal: 14.w),
             child: Align(
               alignment: Alignment.centerLeft,
               child: Text((controller.country?.title?? Country.indonesia.title).tr,style: TextStyle(fontSize: AppDimen.sp_19),
                 textAlign: TextAlign.start,),
             ),
           ),
           Positioned(
               right: 0,
               child: Container(
                 width: 61.w,
                 height: 61.w,
                 padding: EdgeInsets.symmetric(horizontal: 20.w),
                 child: Transform.rotate(
                     angle:-math.pi/2 ,
                     child: const Icon(CupertinoIcons.back)),

               )),
         ]).onTap(()=>controller.showCountries(ctx));
       }),
        TextButton(
            style: AppStyle.app_btn_light,
            onPressed: controller.confirmCountry,
            child: Text(
              "confirm".tr,
            )).size(width: double.infinity, height: 66.w).marginOnly(top: 111.w)
      ],
    ).marginOnly(left: 152.w, right: 152.w, top: 161.w);
  }

  Widget _loginWidget(BuildContext context) {
    return formWidget(context).marginOnly(left: 152.w, right: 152.w, top: 161.w);
  }

  Widget formWidget(BuildContext context) {
    var topMargin = 111.w;

    return Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        Obx(() => controller.storeIdCtr.value == null
            ? Container()
            : genFormItem(controller.storeIdCtr.value!,
                inputType: TextInputType.number, maxLength: 16)),
        genFormItem(controller.jbIdCtr, maxLength: 5),
        Stack(children: [
          genFormItem(controller.pwdCtr, maxLength: 16, inputType: TextInputType.number,obscureText: controller.obscureText),
          Positioned(
              right: 0,
              child: GestureDetector(
                onTap: controller.exEye,
                child: Container(
                  width: 61.w,
                  height: 61.w,
                  padding: EdgeInsets.symmetric(horizontal: 20.w),
                  child: controller.obscureText
                      ? MyAssets.images.icEyeClose.image()
                      : MyAssets.images.icEye.image(),
                ),
              )),
        ]),
        Obx(() {
          var onP = controller.enableLogin.value ? controller.onLogin : null;
          return TextButton(
              style: AppStyle.app_btn_light,
              onPressed: onP,
              child: Text(
                "confirm".tr,
              )).size(width: double.infinity, height: 66.w).marginOnly(top: topMargin);
        }),
        if (controller.startFromHome)
          TextButton(
              style: AppStyle.app_btn,
              onPressed: () {
                Get.back();
              },
              child: Text(
                "cancel".tr,
              )).size(width: double.infinity, height: 66.w).marginOnly(top: 30.w),
        if (!controller.startFromHome)
        Obx(() => controller.storeIdCtr.value != null
            ? Container()
            : Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                    onTap: controller.goHome,
                    child: Text(
                      "ig_login".tr,
                      style: const TextStyle(
                        color: AppColors.e3a671,
                        decoration: TextDecoration.underline, // 设置下划线
                        decorationColor: AppColors.e3a671,
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: controller.goStoreLogin,
                    child: Text(
                      "ex_store".tr,
                      style: const TextStyle(
                        color: AppColors.e3a671,
                      ),
                    ),
                  ),
                ],
              ).marginOnly(top: 31.w)),
      ],
    );
  }

  Widget genFormItem(EdCtr edCtr,
      {TextInputType? inputType, int? maxLength, bool obscureText = false}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          height: 61.w,
          decoration: BoxDecoration(
              color: AppColors.white, borderRadius: BorderRadius.all(Radius.circular(10.w))),
          padding: EdgeInsets.symmetric(horizontal: 14.w),
          child: TextField(
            onChanged: (v) {
              controller.onEdTextChange();
            },
            keyboardType: inputType,
            maxLength: maxLength,
            controller: edCtr.ctr,
            enabled: edCtr.enabled,
            obscureText: obscureText,
            decoration: InputDecoration(
                hintStyle: const TextStyle(color: AppColors.text_666),
                hintText: edCtr.hint,
                counterText: "",
                border: const OutlineInputBorder(borderSide: BorderSide.none),
                contentPadding: const EdgeInsets.only(left: 4, top: 0, bottom: 0, right: 0)),
          ).center(),
        ),
        Obx(() => Text(
              edCtr.eMsg.value,
              style: const TextStyle(color: AppColors.text_e_meg),
            ).marginOnly(left: 14.w))
      ],
    ).size(height: 124.w);
  }
}


class CountrySelect extends StatelessWidget {
  LoginController controller  =Get.find<LoginController>();

  CountrySelect({super.key});
  @override
  Widget build(BuildContext context) {
   return  Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Transform.rotate(
            angle: -math.pi,
            child: MyAssets.images.icTri
                .image(height: 16.w, color: Colors.white, fit: BoxFit.contain)).marginOnly(left: 58.w),
        Container(
          width: 500.w,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8.w),
          ),
          padding: EdgeInsets.symmetric(vertical: 9.w,horizontal: 13.w),
          child: Column(
            children:
            Country.values.map((e){
              return  Column(
                children: [
                  Text(e.title.tr).size(width: double.infinity).marginSymmetric(vertical: 10.w),
                  Container(height: 1.w,color:AppColors.df)
                ],
              ).onTap((){
                controller.selectCountry(e);
              });

            }).toList()
            ,
          ),
        )
      ],
    );
  }

}
